<?php
namespace app\admin\controller;
use think\Controller;
error_reporting(E_ERROR | E_PARSE );
class Init extends Controller
{
	function _initialize()
    {

    	header("content-type:text/html;charset=utf-8");
        $array = array();
       
        $array[]="127.0.0.1";
        if(!in_array($_SERVER['SERVER_NAME'],$array))
        {
            echo "域名不匹配";
            exit;
        }
        if(!$this->is_login())
        {
            $this->redirect('admin/login','', 1, '暂未登录');
        }
        else{

            $Power =model('power');

            $where = ' 1=1';
            $_SESSION['power']='all';
            if($_SESSION['power'] != 'all')
            {
                $where .= '  and power_id in( '.$_SESSION['power'].')';
            }
            $po_par  =  $Power->where("$where and parent_id=0" )->order('sort')->select();
            $po_sub  =  $Power->where("$where and parent_id<>0")->order('sort')->select();
            $po_par =  json_decode( json_encode( $po_par),true);
            $po_sub =  json_decode( json_encode( $po_sub),true);
// var_dump($Power->getData($po_sub));

            foreach ($po_par as $ks => $vs ) {


                foreach($po_sub as $k => $v)
                {
                    if($v['parent_id'] == $vs['power_id'])
                    {
                    	/*$po_par[$ks]['xia']='';*/
                        $po_par[$ks]['xia'][]=$v;
                    }
                }

            }
            
            $this->assign('admin_power',$po_par);


        }
    }

       //是否登录
    public function is_login()
    {
    	return true;
        if($_SESSION['is_login'] !=1)
        {
            return false;
        }
        else{
            $Web = model("web"); // 实例化User对象
            $info = $Web->where("code ='logo'")->select();
            $_SESSION['logo'] = $info[0]['value'];
            return true;
        }


    }

    //验证权限
    public function check_power($power)
    {
        if($_SESSION['power'] == 'all')
        {
            return true;
        }
        else{
            if($power=='admin')
            {
                $this->redirect('index/index','', 1, '暂无权限');
            }
            $Power = model("power"); // 实例化User对象
            $power_id = $info = $Power->where("code='$power'")->field("power_id")->select();
            $power_id = $power_id[0]['power_id'];

//            strstr($_SESSION['power'],'all');
            $po = explode(',',$_SESSION['power']);
//            print_R($po);exit;
            $re = in_array($power_id,$po);
            if($re){
                return true;
            }
            else{
                $this->redirect('index/index','', 1, '暂无权限');
            }



        }


    }


}
